package com.hxzy.controller.admin.system;

import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.entity.DictType;
import com.hxzy.common.core.page.TableDataInfo;
import com.hxzy.common.utils.enums.BusinessType;
import com.hxzy.common.utils.enums.OperatorType;
import com.hxzy.system.dto.DictTypeDTO;
import com.hxzy.system.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 通用字典值查询
 *
 * @author tonneyyy
 */
@RequestMapping(value = "/admin")
@RestController
public class DictTypeController extends BaseController {

    @Autowired
    private DictTypeService  dictTypeService;

    /**
     * 查询字典类型
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping(value = "/dict/type/list")
    public TableDataInfo dataInfo(DictTypeDTO  dictTypeDTO){
        //开启分页
       super.startPage();
        List<DictType> dictTypeList = this.dictTypeService.select(dictTypeDTO);

        return super.getDataTable(dictTypeList);
    }

    /**
     * 新增保存
     * @param dictType
     * @return
     */
    @Log(title = "字典类型",businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping(value = "/dict/type")
    public AjaxResult addSave( @RequestBody DictType dictType){

        dictType.setCreateTime(new Date());
        dictType.setUpdateTime(new Date());
        dictType.setCreateBy(super.getUsername());
        dictType.setUpdateBy(super.getUsername());

        int count=this.dictTypeService.insert(dictType);
        return super.toAjax(count);
    }


    /**
     * 修改保存
     * @param dictType
     * @return
     */
    @Log(title = "字典类型",businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping(value = "/dict/type")
    public AjaxResult updateSave( @RequestBody DictType dictType){
        dictType.setUpdateTime(new Date());
        dictType.setUpdateBy(super.getUsername());

        int count=this.dictTypeService.updateByPrimaryKeySelective(dictType);
        return super.toAjax(count);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/dict/type/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {
        return AjaxResult.success(dictTypeService.selectByPrimaryKey(dictId));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/dict/type/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        return toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
    }

}
